<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
// import resize from './mixins/resize'

const animationDuration = 6000

export default {
  // mixins: [resize],
  props: {
    id: {
      type: String,
      default: 'chart'
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    barData: {
      type: Object,
      default: Object
    },
    itemStyle: {
      type: Object,
      default: Object
    },
    emphasis: {
      type: Object,
      default: Object
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    const that = this
    this.chart = echarts.init(document.getElementById(this.id))

    window.onresize = function() {
      that.chart.resize()
    }

    this.$nextTick(() => {
      this.initChart()
      this.setOptions(this.barData, this.itemStyle, this.emphasis)
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart.setOption(this.barData)
    },
    setOptions({ expectedData, actualData } = {}, itemStyle, emphasis) {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: { // 控制位置
          top: '15%',
          left: '5%',
          right: '5%',
          bottom: '10%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: actualData,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel: { // x轴文字
            textStyle: {
              color: '#5A5A5A'
            },
            interval: 0,
            rotate: 0 // 文字倾斜
          },
          axisLine: { // x轴线
            show: false,
            lineStyle: {
              color: 'pink'
            }
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          },
          axisLabel: { // y轴文字
            textStyle: {
              // color: '#5A5A5A'
            }
          },
          axisLine: { // y轴线
            show: true,
            lineStyle: {
              // color: '#BDE4FF'
            }
          }
        }],
        series: [{
          name: '',
          type: 'bar',
          stack: 'vistors',
          barWidth: '10%',
          data: expectedData,
          animationDuration,
          itemStyle: { // 柱子颜色
            normal: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#0087FE' }
              ]),
              label: {
                show: true, //
                position: 'top',
                textStyle: {
                  color: '#C8C8C8' // 柱子上面数字的颜色
                }
              }
            }
          }
          // emphasis: { // 鼠标经过样式
          //   itemStyle: {
          //     color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          //       { offset: 0, color: '#0083FF' },
          //       { offset: 0.7, color: '#36C7E2' }
          //     ])
          //   }
          // },
        }]
      })
    }
  }
}
</script>
